package cn.hicard.risk.dao.bd.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.hicard.risk.dao.BaseDao;
import cn.hicard.risk.dao.bd.PeriodFreezeDao;
import cn.hicard.risk.entity.BdPeriodfreeze;

@Repository
public class PeriodFreezeDaoImpl extends BaseDao<BdPeriodfreeze> implements PeriodFreezeDao{
	/**
	 * 保存冻结数据
	 */
	public void save(BdPeriodfreeze periodfreeze) throws Exception{
		super.save(periodfreeze);
	}
	
	/**
	 * 更新
	 * @param list
	 * @throws Exception
	 */
	public void updateList(List<BdPeriodfreeze> list) throws Exception {
		super.update(list);	
	}
	
	/**
	 * 根据条件查询交易数据集合
	 */
	public List<BdPeriodfreeze> findList(BdPeriodfreeze start, BdPeriodfreeze end) throws Exception{
		String hql = "FROM BdPeriodfreeze WHERE 1=1";
		List<Object> params = new ArrayList<Object>();
		
		String merchno = start.getWpmerchno();
		String sysno = start.getSysno();
		//String datatype = start.getDatatype();
		
		String transdateStart = start.getTransdate();
		String transdateEnd = end.getTransdate();
		
		String procdateStart = start.getProcdate();
		String procdateEnd = end.getProcdate();
		
		String createdateStart = start.getCreatedate();
		String createdateEnd = end.getCreatedate();
		
		String isFreeze = start.getIsfreeze();
		
		if(merchno != null && merchno.length()>0){
			hql += " AND wpmerchno=?";
			params.add(merchno);
		}
		if(sysno != null && sysno.length()>0){
			hql += " AND sysno=?";
			params.add(sysno);
		}
		/*if( ! "all".equals(datatype)){
			hql += " AND datatype=?";
			params.add(datatype);
		}*/
	
		if(transdateStart != null && transdateStart.length()>0){
			hql += " AND transdate>=?";
			params.add(transdateStart);
		}
		if(transdateEnd != null && transdateEnd.length()>0){
			hql += " AND transdate<=?";
			params.add(transdateEnd);
		}		
		if(procdateStart != null && procdateStart.length()>0){
			hql += " AND procdate>=?";
			params.add(procdateStart);
		}
		if(procdateEnd != null && procdateEnd.length()>0){
			hql += " AND procdate<=?";
			params.add(procdateEnd);
		}
		
		if(createdateStart!=null && createdateStart.length()>0){
			hql += " AND createdate>=?";
			params.add(createdateStart);
		}
		
		if(createdateEnd!=null && createdateEnd.length()>0){
			hql += " AND createdate<=?";
			params.add(createdateEnd);
		}
		
		if(isFreeze != null && isFreeze.length()>0){
			hql += " AND isfreeze=?";
			params.add(isFreeze);
		}
				
		return super.findByHql(hql, params.toArray());
	}
	

	//获取上一个工作日的冻结时间
	//条件date格式为yyyy-MM-dd
	public List<String> findLastDate(String date) throws Exception{
		
		String sql = "select max(createdate) from bd_periodfreeze where cast(createdate as datetime) not between '"+date+" 00:00:00' and '"+date+" 23:59:59'";
		return super.findFieldsBySql(sql);
	}
	
	
	public void updateFreezeState(BdPeriodfreeze periodfreeze) throws Exception{
		String isfreeze = periodfreeze.getIsfreeze();
		String procdate = periodfreeze.getProcdate();
		String createdate = periodfreeze.getCreatedate();
		
		String hql = "update BdPeriodfreeze set isfreeze=?,procdate=? where createdate like ?";
		super.updateByHqlBulk(hql, isfreeze,procdate,createdate+"%");
	}
	
	public boolean existPos(int pkTradefileinfo) throws Exception {
		String hql = "SELECT COUNT(*) FROM BdPeriodfreeze WHERE datatype='pos' AND pkTradefileinfo=?";
		return super.findCountByHql(hql, pkTradefileinfo) > 0;
	}

}
